package com.xsy.de.restful.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xsy.de.entity.User;
import com.xsy.de.service.UserService;
import io.swagger.annotations.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/rest")
@Api(tags = "用户操作接口", description = "用户操作接口")
public class UserRestController {
    private final static Logger log = LogManager.getLogger(UserRestController.class);

    @Autowired
    private UserService userService;

    /**
     * 添加用户
     * @param user
     * @return
     */
    @ApiOperation(value="添加用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value = "/user", method = RequestMethod.POST, produces= MediaType.APPLICATION_JSON_UTF8_VALUE)
    public boolean addUser(@RequestBody User user) {
        user.setStatus("1");
        user.setCreateDate(LocalDateTime.now());
        return userService.insert(user);
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @ApiOperation(value="更新用户信息", notes="根据传入的User对象中的id来更新对应的用户信息，不更新的字段传空即可")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value = "/user", method = RequestMethod.PUT, produces= MediaType.APPLICATION_JSON_UTF8_VALUE)
    public boolean updateUser(@RequestBody User user) {

        return userService.updateById(user);
    }

    /**
     * 删除用户
     * @param id 用户ID
     */
    @ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    public boolean deleteUser(@PathVariable("id") Long id) {

        return userService.deleteById(id);
    }

    /**
     * 根据用户名查找用户信息
     * @param username 用户名
     * @return
     */
    @ApiOperation(value="查找用户", notes="根据url的username来查找用户信息")
    @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/user/name/{username}", method = RequestMethod.GET)
    public User findByUserName(@PathVariable("username") String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("USER_NAME", username);
        return userService.selectOne(queryWrapper);
    }

    /**
     * 根据用户ID查找用户信息
     * @param id 用户ID
     * @return
     */
    @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/user/id/{id}", method = RequestMethod.GET)
    public User findByUserId(@PathVariable("id") Long id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("ID", id);
        return userService.selectOne(queryWrapper);
    }

    /**
     * 获取所有用户信息
     * @return
     */
    @ApiOperation(value="获取用户列表", notes="")
    @RequestMapping(value = "/user/list", method = RequestMethod.GET)
    public List<User> getUserList() {

        return userService.selectList(null);
    }


}
